import { defineStore } from 'pinia';
import { ref } from 'vue';
import Cookies from 'js-cookie';

// 定义 cookieStorage 存储
const cookieStorage = {
    setItem(key, state) {
        return Cookies.set(key, state, { expires: 3 });
    },
    getItem(key) {
        return Cookies.get(key);
    },
};


// state  getters(计算属性)  actions
export const useLoginStore = defineStore('login', () => {
    // 定义 state： 1. 声明变量 2. 返回对象
    const username = ref('');
    const password = ref('');
    // 定义 getters
    const getUsername = () => {
        return username.value.trim();
    }
    const getKfmUsername = () => {
        return 'kfm-' + username.value.trim();
    }
    // 定义 actions
    function setUsername(val) {
        if (val) {
            username.value = val;
        }
    }
    return { username, getUsername, setUsername };
}, {
    // 用于持久化数据
    persist: {
        enabled: true,
        strategies: [
            // {
            //     key: 'login-store',
            //     storage: localStorage,
            //     paths: ['username'],
            // },
            // {
            //     key: 'login-store',
            //     storage: sessionStorage,
            //     paths: ['username', 'name'],
            // },
            // {
            //     key: 'login-store-name',
            //     storage: sessionStorage,
            //     paths: ['name'],
            // },
            {
                key: 'login-store-name',
                storage: cookieStorage,
            },
        ],
    }
});
